package com.arelikebrothers.demo.spring.ioc.life;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.beans.PropertyDescriptor;
import java.io.IOException;

/**
 * Created by lennon on 24/05/2017.
 */
public class BeanFactoryLifeCircle {
    public static void main(String[] args) throws IOException {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions(new PathMatchingResourcePatternResolver().getResources("classpath:*Context.xml"));
        beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
            @Override
            public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
                System.out.println("postProcessBeforeInitialization:" + o + " " + s);
                return o;
            }

            @Override
            public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
                System.out.println("postProcessAfterInitialization:" + o + " " + s);
                return o;
            }
        });

//        beanFactory.addBeanPostProcessor(new InstantiationAwareBeanPostProcessor() {
//            @Override
//            public Object postProcessBeforeInstantiation(Class<?> aClass, String s) throws BeansException {
//                System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation" + aClass + " " + s);
//                return null;
//            }
//
//            @Override
//            public boolean postProcessAfterInstantiation(Object o, String s) throws BeansException {
//                System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation " + o + " " + s);
//                return false;
//            }
//
//            @Override
//            public PropertyValues postProcessPropertyValues(PropertyValues propertyValues, PropertyDescriptor[] propertyDescriptors, Object o, String s) throws BeansException {
//                System.out.println("postProcessPropertyValues " + propertyValues + " " + propertyDescriptors);
//                return propertyValues;
//            }
//
//            @Override
//            public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
//                System.out.println("postProcessBeforeInitialization");
//                return o;
//            }
//
//            @Override
//            public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
//                System.out.println("postProcessAfterInitialization");
//                return o;
//            }
//        });
        Car bean = beanFactory.getBean(Car.class);
        System.out.println(bean);
        beanFactory.destroySingletons();

    }
}
